package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.itheima.exception.BusinessRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @ClassName ValidateCodeServiceImpl
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-28
 * @Version V1.0
 **/
@Slf4j
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
    String accessKey = "LTAI4FepKkpHudL8jgizca93";
    String secretKey = "SYmJVdyH7UwStDMiAtWvLJqCSE0kVe";
    String signName = "健康人生";
    String validateCodeTemplateCode = "SMS_173344428";
    String commonTemplateCode = "SMS_190267568";

    @Override
    public void sendValidateCodeShortMessage(String telephone, String type, String validateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, secretKey);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", validateCodeTemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + validateCode + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            if (response.getHttpStatus() == 200) {
                log.debug("发送验证码成功");
                //将验证码存入redis
                saveValidateCodeToRedis(telephone, type, validateCode);
            } else {
                log.debug("发送验证码失败");
                throw new BusinessRuntimeException("发送验证码失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("发送验证码短信失败");
            throw new BusinessRuntimeException("发送验证码短信失败!");
        }
    }

    @Autowired
    JedisPool jedisPool;

    //将验证码存入redis中
    private void saveValidateCodeToRedis(String telephone, String type, String validateCode) {
        Jedis jedis = jedisPool.getResource();
        jedis.setex(type + "-" + telephone, 5 * 60, validateCode);
    }

    //校验验证码
    @Override
    public boolean checkValidateCode(String telephone, String type, String validateCode) {
        Jedis jedis = jedisPool.getResource();
        String validateCodeInRedis = jedis.get(type + "-" + telephone);
        if (validateCodeInRedis != null && validateCodeInRedis.equalsIgnoreCase(validateCode)) {
            log.debug("校验验证码成功");
            return true;
        }
        log.debug("校验验证码失败!");
        return false;
    }

    @Override
    public void sendCommonShortMessage(String telephone, String param) {

    }
}
